﻿using Chain.BLL;
using Chain.DBUtility;
using Chain.Model;
using System;
using System.Text;


public class MEMSmallChangeUpdate
{
    private static Chain.BLL.Mem bll = new Chain.BLL.Mem();
    private static Chain.BLL.Mem_Rebate BLLMemRebate = new Chain.BLL.Mem_Rebate();
    private static string MEMCard;
    private static string MEMName;
    private static bool RateType;
    private static decimal[] MEMRate = new decimal[3];
    private static int NowRateLevel;
    private static int RateLevel;
    private static string PointOrderCode;
    public static void MEMSmallChange(Chain.Model.Mem mem, decimal orderMoney, string OrderAccount, int orderType, int UserID)
    {
        Chain.Model.SysUser User = new Chain.BLL.SysUser().GetModel(UserID);
        Chain.Model.Mem_Rebate MemRebate = BLLMemRebate.GetModel("MemID=" + mem.MemRecommendID + " and isonline=1 and StartTime<getdate() and EndTime>getdate() order by StartTime desc");
        //Chain.Model.SysParameter modelSysParameter = new Chain.BLL.SysParameter().GetModel(1); 积分的开关
        RateLevel = BLLMemRebate.GetModelLevel(MemRebate);
        NowRateLevel = 1;
        PointOrderCode = OrderAccount;
        MEMCard = mem.MemCard;
        MEMName = mem.MemName;
        //if (User != null && modelSysParameter.PointLevel)
        if (User != null && RateLevel!=0)
        {
            MEMRate[0] = MemRebate.Level1??0;
            MEMRate[1] = MemRebate.Level2??0;
            MEMRate[2] = MemRebate.Level3??0;
            SmallChangeUpdate(mem, orderMoney, orderType, mem.MemID, User);
        }
    }

    public static void SmallChangeUpdate(Chain.Model.Mem mem, decimal orderMoney, int orderType, int mymemID, Chain.Model.SysUser User)
    {
        if ((NowRateLevel <= RateLevel) && (mem.MemRecommendID != 0))
        {
            Chain.Model.Mem TEMPMEM = bll.GetModel(mem.MemRecommendID);

            if (TEMPMEM != null)
            {
                decimal temp = decimal.Truncate(orderMoney * (MEMRate[NowRateLevel - 1] / 100M));
                TEMPMEM.SmallChange+=temp;
                    int flag = bll.Update(TEMPMEM);
                    if (flag == 1)
                    {
                        PubFunction.UpdateMemLevel(TEMPMEM);
                        Chain.Model.SmallChangeLog modelLog = new Chain.Model.SmallChangeLog();
                        modelLog.SmallChangeMemID = TEMPMEM.MemID;
                        modelLog.SmallChangeNumber = temp;
                        modelLog.SmallChangeChangeType = 1;
                        modelLog.SmallChangeShopID = TEMPMEM.MemShopID;
                        modelLog.SmallChangeCreateTime = DateTime.Now;
                        modelLog.SmallChangeUserID = TEMPMEM.MemUserID;
                        modelLog.SmallChangeOrderCode = PointOrderCode;
                        modelLog.SmallChangeGiveMemID = mymemID;
                        switch (orderType)
                        {
                            case -1:
                                modelLog.SmallChangeRemark = "卡号为：" + MEMCard + ",姓名为" + MEMName + "的会员商品消费退货，获得零钱：" + temp.ToString();
                                break;
                            case 1:
                                modelLog.SmallChangeRemark = "卡号为：" + MEMCard + ",姓名为" + MEMName + "的会员商品消费，获得零钱：" + temp.ToString();
                                break;
                            case 2:
                                modelLog.SmallChangeRemark = "卡号为：" + MEMCard + ",姓名为" + MEMName + "的会员快速消费，获得零钱：" + temp.ToString();
                                break;
                        }
                        new Chain.BLL.SmallChangeLog().Add(modelLog);
                        PubFunction.SaveSysLog(TEMPMEM.MemUserID, 3, "消费返利", "会员" + TEMPMEM.MemName + "获取返利成功", TEMPMEM.MemShopID, DateTime.Now, PubFunction.ipAdress);
                    }
                    NowRateLevel++;
                    SmallChangeUpdate(TEMPMEM, orderMoney, orderType, mymemID, User);   
            }
        }
    }
}
